// pages/xuecar/learncar.js
const app = getApp()
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
    data: {
      //  底部导航
      listInfo: [],
        imgUrls: [],
        inputShowed: false,
        inputVal: "",
        loading: true,
        parentid: 0,
        teacherlist:[]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        page = 1;
        this.initBanner();
        this.getTeachers();
        this.chkuptrs();
    },
    //滚动到底部刷新
    onReachBottom: function () {
        this.getTeachers();
    },
    //检查报名人上级是否是教练
    chkuptrs:function(){
      var that=this;
      var openid = wx.getStorageSync('openId');
      var requrl = app.globalData.hostUrl + 'common/chkUptrs';
      wx.request({
        url: requrl,
        data: { 'openid': openid },
        success: function (res) {
          if (res.data.code == 200) {
            that.setData({
              parentid: res.data.result
            })
          }
        }
      })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.setData({
        listInfo: app.globalData.listInfo
      })
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('indexBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('indexBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '101' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        //console.log(res);
                        wx.setStorageSync('indexBanner', res.data.result);
                        that.setData({
                            imgUrls: res.data.result
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                imgUrls: imgbanner
            })
        }
    },
    //获取教练列表
    getTeachers: function () {
        var that = this;
        var limit = 10;
        var requrl = app.globalData.hostUrl + 'common/TeacherByrid';
        var keyname = 'teacher_' + page + '_' + limit;//获取第1页的前面3条；
        var teachers = wx.getStorageSync(keyname);
        if (!app.expiratTime(keyname) || teachers.length == 0) {
            wx.request({
                url: requrl,
                data: { 'page': page, 'psize': limit, 'rid': '教练' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    //console.log(res);
                  if (res.data.code == 200) {
                        wx.setStorageSync(keyname, res.data.result);
                        that.setData({
                            teacherlist: that.data.teacherlist.concat(res.data.result)
                        })
                        if (res.data.result.length < limit) {
                            that.setData({
                                loading: false
                            })
                        }
                    }
                }
            })
        }
        else {
            page++;
            that.setData({
                teacherlist: teachers,
                loading: false
            })
        }

    },
    singup: function (e) {
        //console.log(e);
        var tid=e.currentTarget.dataset.tid;
        var formId = e.detail.formId;
        var openid = wx.getStorageSync('openId');
        var requrl = app.globalData.hostUrl + 'common/getFormid';
        app.saveformid(formId, openid, 'xc', requrl);
        if (parseInt(tid)==0){
          tid = this.data.parentid;
        }
        
        wx.navigateTo({
            url: '/pages/xuecar/signup?tid='+tid,
        })
    },
//================================搜索框事件开始==============================
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    //跳转到搜索页面
    srckeyword: function () {
        var keyword = this.data.inputVal;
        this.setData({
            inputVal: "",
            inputShowed: false
        });
        wx.navigateTo({
            url: '../search/index?keyword=' + keyword,
        }) 
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
    },
//================================搜索框事件结束==============================
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/thappointment',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myappointment',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/trorder',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myorder',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})